package com.itcast.donkey.fragments;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.itcast.donkey.R;

import com.itcast.donkey.adapters.InHeadTravelAdapter;
import com.itcast.donkey.modles.TravelInHeadImageModels;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/9/12.
 */
public class InHeadTravelFragment extends Fragment {
    private XRecyclerView xRecyclerView;
    private InHeadTravelAdapter travelAdapter;

    private SharedPreferences sp;
    private String ID;
    //上拉加载和下拉刷新
    private int page = 1;
    //真实值
    private List<TravelInHeadImageModels.TripsBean> travelInHeadImageModelsList = new ArrayList<>();
    //中间值
    private List<TravelInHeadImageModels.TripsBean> temptravelInHeadImageModelsList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_in_head_travel, null);
        xRecyclerView = (XRecyclerView) view.findViewById(R.id.xRecyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xRecyclerView.setLayoutManager(layoutManager);

        //上拉加载的动画效果
        xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        //recyclerView.setLaodingMoreProgressStyle(ProgressStyle.Pacman);
        //下拉刷新的动画效果
        xRecyclerView.setArrowImageView(R.drawable.ic_loading_rotate);

        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            //下拉刷新
            @Override
            public void onRefresh() {
                page = 1;
                initData();
            }

            //上拉加载
            @Override
            public void onLoadMore() {
                page++;
                initData();
            }
        });
        //接收值
        sp = getActivity().getSharedPreferences("config", Context.MODE_PRIVATE);
        ID = sp.getString("headimage", "11");

        initData();
        initEvent();
        return view;
    }

    private void initEvent() {

    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url("http://chanyouji.com/api/users/" + ID + ".json?page=" + page)

                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();

                        TravelInHeadImageModels head = gson.fromJson(result, TravelInHeadImageModels.class);
                        temptravelInHeadImageModelsList = head.getTrips();
                        if (temptravelInHeadImageModelsList.size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(2);
                        }
                    }
                });
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(getActivity(), "头像请求失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    if (page == 1) {
                        travelInHeadImageModelsList.clear();
                        travelInHeadImageModelsList.addAll(temptravelInHeadImageModelsList);
                        travelAdapter = new InHeadTravelAdapter(getActivity(), travelInHeadImageModelsList);
                        xRecyclerView.setAdapter(travelAdapter);

                        xRecyclerView.refreshComplete();
                        temptravelInHeadImageModelsList.clear();
                    } else {
                        travelInHeadImageModelsList.addAll(temptravelInHeadImageModelsList);
                        travelAdapter.notifyDataSetChanged();
                        xRecyclerView.loadMoreComplete();
                        temptravelInHeadImageModelsList.clear();
                    }
                    break;
                case 2:
                    Toast.makeText(getActivity(), "加载到底了", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };


}
